package com.cs.study.homework._0305;

import java.io.*;

public class Demo2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //User[] users = new User[10];
        serialize();
        unSerialize();

    }

    private static void unSerialize() throws IOException, ClassNotFoundException {
        ObjectInputStream in =
                new ObjectInputStream(new FileInputStream("E:\\a\\c.txt"));
        Object o = in.readObject();
        System.out.println(o);
        in.close();
    }

    private static void serialize() throws IOException {
        Address address = new Address("123", "123");
        User user = new User(1, "张三", "12345678945", address);
        // 创建输出流对象
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("E:\\a\\c.txt"));
        // writeObject(Object o)
        out.writeObject(user);
        // close
        out.close();
    }
}


class User implements Serializable {
    int id;
    String name;
    String mobile;
    Address address;

    public User(int id, String name, String mobile, Address address) {
        this.id = id;
        this.name = name;
        this.mobile = mobile;
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", mobile='" + mobile + '\'' +
                ", address=" + address +
                '}';
    }
}

class Address implements Serializable{
    String detail;
    String code;

    public Address(String detail, String code) {
        this.detail = detail;
        this.code = code;
    }


}
